package com.zj.aspects;

import com.zj.web.bean.MyUserDTO;
import com.zj.web.controller.AuthResponse;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Log
@Order(10)
public class UserLoginAspect {
    @Autowired
    private JmsMessageProducter jmsMessageProductr;


    //切入点表达式
    @Pointcut("execution(* com.zj.service.UserServiceImpl.login(..))")
    public void loginPointcut() {
    }
    //通知的类型 before after afterReturning afterThrowing around
    @AfterReturning(value = "loginPointcut()",returning = "obj")
    public void sendEmailMessageToMQ(JoinPoint jp,Object obj){ //jp是连接点，即原登录方法 ，通过Jp可以获取到登录方法的参数，返回值，异常信息
        log.info("登录成功成功后，发送一条要发送给邮件信息到MQ");
        if (obj==null && !(obj instanceof AuthResponse)){
            return;
        }
        AuthResponse authResponse = (AuthResponse) obj;
        MyUserDTO myUserDTO = authResponse.getMyUserDTO();
        log.info("登录成功的用户信息："+myUserDTO.toString());
        jmsMessageProductr.sendMessage(myUserDTO);
    }
}
